﻿using FlyingEye.ViewModels;
using HandyControl.Tools.Extension;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Windows;

namespace FlyingEye.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IServiceProvider _services;

        private readonly MainWindowViewModel _viewModel;

        public MainWindow(MainWindowViewModel viewModel, IServiceProvider services)
        {
            Loaded += MainWindow_Loaded;
            _viewModel = viewModel;
            this.StateChanged += MainWindow_StateChanged;
            _previousWindowState = this.WindowState;
            this.DataContext = viewModel;
            InitializeComponent();
            _services = services;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var os = Environment.OSVersion;
            this.OSVersionTextBlock.Text = $"软件版本：2.0.3    Windows 版本：{os.Version}";
        }

        private WindowState _previousWindowState;

        public string GetResourceId()
        {
            return ((MainWindowViewModel)this.DataContext).ResourceId;
        }

        private void MainWindow_StateChanged(object? sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
            {
                _previousWindowState = this.WindowState;
                this.NotifyIcon.Hide();
            }
            else
            if (this.WindowState == WindowState.Minimized)
            {
                this.NotifyIcon.Show();
                this.ShowInTaskbar = false;
            }
        }

        private void NotifyIcon_Click(object sender, RoutedEventArgs e)
        {
            this.ShowInTaskbar = true;
            this.WindowState = _previousWindowState;
            this.Show();
            this.Activate();
        }

        private void OpenClick(object sender, RoutedEventArgs e)
        {
            NotifyIcon.CloseContextControl();
            this.ShowInTaskbar = true;
            this.WindowState = _previousWindowState;
            this.Show();
            this.Activate();
        }

        private bool _isFromContentMenuClose = false;

        private bool AuthCurrentUser(Window window)
        {
            var authenticationWindow = _services.GetRequiredService<AuthenticationWindow>();
            authenticationWindow.Left = window.Left + 10;
            authenticationWindow.Top = window.Top + 10;
            var resutl = authenticationWindow.ShowDialog();
            return resutl == true;
        }

        private void ExitClick(object sender, RoutedEventArgs e)
        {
            if (AuthCurrentUser(this))
            {
                _isFromContentMenuClose = true;
                this.Close();
            }
            else
            {
                _isFromContentMenuClose = false;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_isFromContentMenuClose)
            {
                this.Dispatcher.Invoke(() =>
                {
                    Task.Run(async () =>
                    {
                        await _viewModel.CloseAsync();
                    }).Wait();
                });
            }
            else
            {
                if (AuthCurrentUser(this))
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        Task.Run(async () =>
                        {
                            await _viewModel.CloseAsync();
                        }).Wait();
                    });
                }
                else
                {
                    e.Cancel = true;
                }
            }

            base.OnClosing(e);
        }
    }
}
